package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.SolicitudAsignacion;
import co.gov.medellin.sdep.sisdep.model.dao.ISolicitudAsignacionDAO;

public class SolicitudAsignacionDAOImpl extends JdbcDaoSupport implements ISolicitudAsignacionDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM ADM_SOLICITUD_ASIGNACION_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(SolicitudAsignacion solicitudAsignacion) {
		deleteOperation.execute( solicitudAsignacion  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public SolicitudAsignacion findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(SolicitudAsignacion solicitudAsignacion) {
		insertOperation.execute( solicitudAsignacion );
	}

	public void update(SolicitudAsignacion solicitudAsignacion) {
		updateOperation.execute( solicitudAsignacion );
	}

	public SolicitudAsignacion findByCriteria(SolicitudAsignacion  solicitudAsignacion) {
		return findByCriteriaQuery.runQuery(solicitudAsignacion);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public SolicitudAsignacion findByActiveState(SolicitudAsignacion  solicitudAsignacion) {
		return findByActiveStateQuery.runQuery(solicitudAsignacion);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//Desabilitado
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//findAllQuery = new FindAllQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO ADM_SOLICITUD_ASIGNACION_T ("
				+ " ID, "
				+ " ID_SOLICITUD, "
				+ " ID_AREA, "
				+ " FECHA_ASIGNACION, "
				+ " COMENTARIO, "
				+ " ESTADO, "
				+ " USUARIO_CREA, "				
				+ " FECHA_CREA "
				+ ") VALUES ("
				+ " ADM_SOLICITUD_ASIGNACION_SEC.nextVal, "
				+ PARAMETER
				+ PARAMETER
				+ "SYSDATE,"
				+ PARAMETER
				+ PARAMETER
				+ PARAMETER
				+ "SYSDATE" 
				+ ")";
		public  InsertOperation(DataSource ds) {
			super(ds, sql);
			// set here required parameters
			declareParameter(new SqlParameter(Types.INTEGER));
			declareParameter(new SqlParameter(Types.INTEGER));
			declareParameter(new SqlParameter(Types.CHAR));
			declareParameter(new SqlParameter(Types.CHAR));
			declareParameter(new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(SolicitudAsignacion solicitudAsignacion) {
			InsertOperation.this.update(new Object[] {
					solicitudAsignacion.getIdSolicitud(),
					solicitudAsignacion.getIdArea(),
					solicitudAsignacion.getComentario(),
					solicitudAsignacion.getEstado(),
					solicitudAsignacion.getUsuarioCrea() });
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE ADM_SOLICITUD_ASIGNACION_T SET "
					  + " ID_SOLICITUD = ? ,"
					  + " ID_AREA = ? ,"
					  + " FECHA_ASIGNACION = SYSDATE ,"
					  + " COMENTARIO = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
		  			  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(SolicitudAsignacion solicitudAsignacion) {
			UpdateOperation.this.update( new Object[]{
				solicitudAsignacion.getIdSolicitud(),
				solicitudAsignacion.getIdArea(),
				solicitudAsignacion.getComentario(),
				solicitudAsignacion.getEstado(),
				solicitudAsignacion.getUsuarioModifica(),
				solicitudAsignacion.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_SOLICITUD_ASIGNACION_T "
										+ WHERE;
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(SolicitudAsignacion solicitudAsignacion) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_SOLICITUD_ASIGNACION_T "
										+ WHERE + " 1=1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			SolicitudAsignacion solicitudAsignacion = new SolicitudAsignacion();
				solicitudAsignacion.setId(new Long(rs.getLong("ID")));
				solicitudAsignacion.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				solicitudAsignacion.setIdArea(new Long(rs.getLong("ID_AREA")));
				solicitudAsignacion.setFechaAsignacion(rs.getDate("FECHA_ASIGNACION"));		
				solicitudAsignacion.setComentario(rs.getString("COMENTARIO"));
				solicitudAsignacion.setEstado(rs.getString("ESTADO"));
				solicitudAsignacion.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				solicitudAsignacion.setFechaCrea(rs.getDate("FECHA_CREA"));		
				solicitudAsignacion.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				solicitudAsignacion.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return solicitudAsignacion;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = "SELECT "
				+ " A.ID                  AS ID, "
				+ " A.RADICADO            AS RADICADO, "
				+ " A.FECHA_RADICADO      AS FECHA_RADICADO, "
				+ " A.CODIGO              AS CODIGO, "
				+ " A.ID_PERSONA          AS ID_PERSONA, "
				+ " A.ID_TIPO_SOLICITUD   AS ID_TIPO_SOLICITUD, "
				+ " A.DESCRIPCION         AS DESCRIPCION, "
				+ " A.DIRECCION_VENTA     AS DIRECCION_VENTA, "
				+ " A.CODIGO_BARRIO       AS CODIGO_BARRIO, "
				+ " A.BARRIO              AS BARRIO, "
				+ " A.CODIGO_COMUNA       AS CODIGO_COMUNA, "
				+ " A.COMUNA              AS COMUNA, "
				+ " A.PRIORIDAD           AS PRIORIDAD, "
				+ " A.ESTADO              AS ESTADO, "
				+ " A.AUTORIZACION_WF_WM  AS AUTORIZACION_WF_WM, "
				+ " A.ID_WF_WM            AS ID_WF_WM, "
				+ " A.USUARIO_CREA        AS USUARIO_CREA, "
				+ " A.FECHA_CREA          AS FECHA_CREA, "
				+ " A.USUARIO_MODIFICA    AS USUARIO_MODIFICA, "
				+ " A.FECHA_MODIFICA      AS FECHA_MODIFICA, "
				+ " B.ID_AREA             AS ID_AREA, "
				+ " C.VRT_NOMBRE          AS AREA, "
				+ " B.FECHA_ASIGNACION    AS FECHA_ASIGNACION, "
				+ " B.COMENTARIO          AS COMENTARIO " + " FROM "
				+ " ADM_SOLICITUD_T A " + " LEFT JOIN "
				+ " ADM_SOLICITUD_ASIGNACION_T B " + " ON "
				+ " A.ID = B.ID_SOLICITUD " + " LEFT JOIN "
				+ " ADM_VALOR_TIPO_MAESTRA_T C " + " ON "
				+ " B.ID_AREA = C.ID_VALOR_TIPO "
				+ " WHERE "
				+ " A.ID = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter(new SqlParameter(Types.INTEGER));
			compile();
    	}
    	public SolicitudAsignacion runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (SolicitudAsignacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					+ " AND ID_SOLICITUD = ? ";
			  	  	  					//+ " AND ID_AREA = ? "
			  	  	  					//+ " AND FECHA_ASIGNACION = ? "
			  	  	  					//+ " AND COMENTARIO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public SolicitudAsignacion runQuery(SolicitudAsignacion solicitudAsignacion) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//solicitudAsignacion.getId(),
				solicitudAsignacion.getIdSolicitud()
				//solicitudAsignacion.getIdArea(),
				//solicitudAsignacion.getFechaAsignacion(),
				//solicitudAsignacion.getComentario(),
				//solicitudAsignacion.getEstado(),
				//solicitudAsignacion.getUsuarioCrea(),
				//solicitudAsignacion.getFechaCrea(),
				//solicitudAsignacion.getUsuarioModifica(),
				//solicitudAsignacion.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (SolicitudAsignacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudAsignacion runQuery(SolicitudAsignacion solicitudAsignacion) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (SolicitudAsignacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
